#!/usr/bin/env python3
"""调试淘宝页面结构"""

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from urllib.parse import quote

def debug_taobao():
    """调试淘宝页面结构"""
    
    # 设置Chrome选项（使用Chromium）
    chrome_options = Options()
    
    # 指定Chromium二进制文件路径
    chrome_options.binary_location = '/usr/bin/chromium'
    
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
    
    # 指定ChromeDriver路径
    from selenium.webdriver.chrome.service import Service
    service = Service('/usr/bin/chromedriver')
    driver = webdriver.Chrome(service=service, options=chrome_options)
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    
    try:
        print("=== 测试淘宝搜索页面 ===")
        
        # 1. 先访问淘宝首页
        print("\n1. 访问淘宝首页...")
        driver.get("https://www.taobao.com")
        time.sleep(5)
        print(f"首页标题: {driver.title}")
        
        # 检查是否有登录相关元素
        login_elements = driver.find_elements(By.CSS_SELECTOR, "[class*='login'], .login, #login")
        print(f"首页登录元素数量: {len(login_elements)}")
        
        # 2. 直接访问搜索URL
        keyword = "数据线"
        encoded_keyword = quote(keyword)
        search_url = f"https://s.taobao.com/search?q={encoded_keyword}&s=0"
        
        print(f"\n2. 访问搜索URL: {search_url}")
        driver.get(search_url)
        time.sleep(10)
        
        print(f"搜索页面标题: {driver.title}")
        print(f"当前URL: {driver.current_url}")
        
        # 检查页面内容
        page_source = driver.page_source
        print(f"页面源码长度: {len(page_source)}")
        
        # 检查是否有登录相关关键词
        login_keywords = ['登录', 'login', '验证', 'verify', '滑块', 'slider']
        found_keywords = []
        for keyword in login_keywords:
            if keyword in page_source.lower():
                found_keywords.append(keyword)
        
        if found_keywords:
            print(f"发现登录相关关键词: {found_keywords}")
        else:
            print("未发现明显的登录相关关键词")
        
        # 3. 测试各种商品选择器
        print("\n3. 测试商品选择器...")
        selectors_to_test = [
            '.item',
            '.Card--doubleCardWrapper--L2XFE73',
            '[data-category="auctions"]',
            '.grid-item',
            '.product',
            '.item-wrapper',
            '.goods-item',
            '[class*="item"]',
            '[class*="Card"]',
            '[class*="product"]',
            'div[data-spm]',
            'a[href*="item.taobao.com"]'
        ]
        
        for selector in selectors_to_test:
            try:
                elements = driver.find_elements(By.CSS_SELECTOR, selector)
                print(f"选择器 '{selector}': 找到 {len(elements)} 个元素")
                
                if elements and len(elements) > 0:
                    # 尝试提取第一个元素的信息
                    first_element = elements[0]
                    try:
                        # 查找标题
                        title_selectors = ['.title', 'a[title]', 'h3', 'span[title]']
                        title = None
                        for title_sel in title_selectors:
                            try:
                                title_elem = first_element.find_element(By.CSS_SELECTOR, title_sel)
                                title = title_elem.get_attribute('title') or title_elem.text.strip()
                                if title:
                                    break
                            except:
                                continue
                        
                        if title:
                            print(f"  -> 第一个元素标题: {title[:50]}...")
                        else:
                            print(f"  -> 第一个元素未找到标题")
                            
                    except Exception as e:
                        print(f"  -> 提取第一个元素信息失败: {e}")
                        
            except Exception as e:
                print(f"选择器 '{selector}': 错误 - {e}")
        
        # 4. 检查页面是否包含商品相关关键词
        print("\n4. 检查页面关键词...")
        product_keywords = ['商品', '价格', '销量', '店铺', '月销', '评价', '包邮']
        found_product_keywords = []
        for keyword in product_keywords:
            if keyword in page_source:
                found_product_keywords.append(keyword)
        
        if found_product_keywords:
            print(f"发现商品相关关键词: {found_product_keywords}")
        else:
            print("未发现商品相关关键词")
        
        # 5. 保存页面截图
        print("\n5. 保存页面截图...")
        driver.save_screenshot("taobao_debug.png")
        print("截图已保存为 taobao_debug.png")
        
        # 6. 尝试其他搜索URL格式
        print("\n6. 测试其他搜索URL格式...")
        alternative_urls = [
            f"https://s.taobao.com/search?q={encoded_keyword}",
            f"https://world.taobao.com/search/search.htm?q={encoded_keyword}",
            f"https://s.taobao.com/list?q={encoded_keyword}"
        ]
        
        for i, url in enumerate(alternative_urls, 1):
            print(f"\n测试URL {i}: {url}")
            driver.get(url)
            time.sleep(5)
            print(f"标题: {driver.title}")
            print(f"当前URL: {driver.current_url}")
            
            # 快速检查是否有商品元素
            quick_selectors = ['.item', '[class*="item"]', 'a[href*="item.taobao.com"]']
            total_elements = 0
            for sel in quick_selectors:
                try:
                    elements = driver.find_elements(By.CSS_SELECTOR, sel)
                    total_elements += len(elements)
                except:
                    pass
            print(f"商品相关元素总数: {total_elements}")
        
    except Exception as e:
        print(f"调试过程中出现错误: {e}")
    
    finally:
        print("\n调试完成，5秒后关闭浏览器...")
        time.sleep(5)
        driver.quit()

if __name__ == "__main__":
    debug_taobao()